package com.czxy.service.common;

import com.czxy.pojo.BasePojo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Date;
import java.util.List;

/**
 * 基础service
 *
 * @author likk
 * Created by User on 2018/7/24.
 */
public class AbstractBaseService<T extends BasePojo> implements BaseService<T> {

    // spring4新特性，支持泛型注入：即将子类的泛型传入到父类中，根据结合父类接口类型以及泛型的类型去IoC容器中找到相应的bean，进行注入
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private Mapper<T> mapper;

    // 当前Service上泛型的字节码对象
    private Class<T> clazz;

    {
        // 读取当前类上的泛型
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz = (Class<T>) type.getActualTypeArguments()[0];
    }

    /**
     * 根据主键进行查询
     * @param id
     * @return
     */
    @Override
    public T queryById(Serializable id){
        return this.mapper.selectByPrimaryKey(id);
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<T> queryAll(){
        return this.mapper.select(null);
    }

    /**
     * 根据条件查询一条数据
     *
     * @param record
     * @return
     */
    @Override
    public T queryOne(T record){
        return this.mapper.selectOne(record);
    }

    /**
     * 根据条件查询多条数据
     *
     * @param record
     * @return
     */
    @Override
    public List<T> queryListByWhere(T record){
        return this.mapper.select(record);
    }

    /**
     * 根据条件分页查询
     *
     * @param record
     * @param page
     * @param rows
     * @return
     */
    @Override
    public PageInfo<T> queryPageListByWhere(T record, Integer page, Integer rows){
        PageHelper.startPage(page, rows);
        List<T> list = this.mapper.select(record);
        return new PageInfo<T>(list);
    }

    /**
     * 插入数据
     *
     * @param record
     * @return
     */
    @Override
    public Integer save(T record){
        record.setCreated(new Date());
        record.setCreated(record.getCreated());
        return this.mapper.insert(record);
    }

    /**
     * 更新数据
     *
     * @param record
     * @return
     */
    @Override
    public Integer update(T record){
        record.setUpdated(new Date());
        return this.mapper.updateByPrimaryKey(record);
    }

    /**
     * 更新数据,只操作record中的非空属性
     * @param record
     * @return
     */
    @Override
    public Integer updateSelective(T record){
        record.setUpdated(new Date());
        // 保证created字段不会被修改
        record.setCreated(null);
        return this.mapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 根据主键进行删除
     * @param id
     * @return
     */
    @Override
    public Integer deleteById(Serializable id){
        return this.mapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Integer deleteByIds(String property, List<Serializable> ids){
        Example example = new Example(clazz);
        example.createCriteria().andIn(property, ids);
        return this.mapper.deleteByExample(example);
    }

    /**
     * 根据条件删除
     * @param record
     * @return
     */
    @Override
    public Integer deleteByWhere(T record){
        return this.mapper.delete(record);
    }

    /**
     * 分页并且排序
     *
     * @param page
     * @param rows
     * @param orderByClause
     * @return
     */
    @Override
    public PageInfo<T> queryPageListAndSort(Integer page, Integer rows, String orderByClause){
        // 开启分页
        PageHelper.startPage(page, rows);
        // 构造查询条件
        Example example = new Example(clazz);
        // 排序
        example.setOrderByClause(orderByClause);
        // 查询
        List<T> list = this.mapper.selectByExample(example);
        // 返回分页对象
        return new PageInfo<T>(list);
    }


}



















